package fhp.hlhj.giantfold.fragment

import android.content.Intent
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.liaoinstan.springview.container.DefaultFooter
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import com.lzy.okgo.model.HttpParams
import fhp.hlhj.giantfold.Contents
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.activity.orders.OrderDetail
import fhp.hlhj.giantfold.adapter.SelfOrderAdp
import fhp.hlhj.giantfold.interfaces.ISelfOrder
import fhp.hlhj.giantfold.javaBean.SelfOrderBean
import fhp.hlhj.giantfold.presenter.SelfOrderPresenter
import fhp.hlhj.giantfold.utils.LogUtil
import kotlinx.android.synthetic.main.self_order.view.*
import java.util.ArrayList

/**
 * author: FHP
 * data: on 2018\1\25 0025 16:01
 * E-Mail: 103945089@qq.com
 */
class OrderFgm :Fragment(), ISelfOrder {

    companion object {
        fun newInstece(name:String): OrderFgm {
            val allGoodsFgm = OrderFgm()
            val bundle = Bundle()
            bundle.putString("name", name)
            allGoodsFgm.arguments = bundle
            return allGoodsFgm
        }
    }

    private lateinit var presenter:SelfOrderPresenter
    private lateinit var rootView:View
    private val datas=ArrayList<SelfOrderBean.DataBean>()
    private val orderAdp = SelfOrderAdp(R.layout.self_order_item, datas)

    private var page=0
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        rootView=inflater.inflate(R.layout.self_order,null)
        presenter= SelfOrderPresenter(this)
        presenter.setAdp()
        when(arguments.getString("name")){
            "全部"->{
                val params = HttpParams()
                params.put("user_id",Contents.userId)
                params.put("page",page)
                presenter.getSelfOrder(params)
            }
            "进行中"->{
                val params = HttpParams()
                params.put("user_id",Contents.userId)
                params.put("page",page)
                params.put("status",2)
                presenter.getSelfOrder(params)
            }
            "已存入"->{
                val params = HttpParams()
                params.put("user_id",Contents.userId)
                params.put("page",page)
                params.put("deposit_status",1)
                presenter.getSelfOrder(params)
            }
            "无效"->{
                val params = HttpParams()
                params.put("user_id",Contents.userId)
                params.put("page",page)
                params.put("status",4)
                presenter.getSelfOrder(params)
            }
            "待翻牌"->{
                val params = HttpParams()
                params.put("user_id",Contents.userId)
                params.put("page",page)
                params.put("fanpai_status",0)
                presenter.getSelfOrder(params)
            }
        }

        return rootView
    }

    override fun showLoading() {

    }
    override fun setAdp() {
        val linearLayoutManager = LinearLayoutManager(context, LinearLayout.VERTICAL, false)
        rootView.recyclerview.layoutManager=linearLayoutManager
        rootView.recyclerview.adapter=orderAdp
        rootView.spView.header=DefaultHeader(context)
        rootView.spView.footer=DefaultFooter(context)
        rootView.spView.setListener(object :SpringView.OnFreshListener{
            override fun onLoadmore() {
                page++
                val params = HttpParams()
                params.put("user_id",Contents.userId)
                params.put("page",page)
                presenter.getSelfOrder(params)
            }

            override fun onRefresh() {
            }
        })
        orderAdp.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(context, OrderDetail::class.java)
            intent.putExtra("oid",datas[position].id)
            context.startActivity(intent)
        }
    }

    override fun getSelfOrders(selfOrderBean: SelfOrderBean) {
        rootView.spView.onFinishFreshAndLoad()
        if (selfOrderBean.code==200){
            LogUtil.log("设置列表${selfOrderBean.data.size}")
            datas.addAll(selfOrderBean.data)
            orderAdp.notifyDataSetChanged()
        }

    }
}